package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daimajia.swipe.SwipeLayout;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.CheckWorkDetailListItemBinding;
import com.shenyue.nbtownlet.util.StringUtil;

public class CheckWorkDetailViewAdapter extends BaseSwipeAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();

    public interface CheckWorkDetailListItemClickListener {
        void onItemClick(int position);

        void onItemDelClick(int position);
    }

    private CheckWorkDetailListItemClickListener listener;

    public CheckWorkDetailViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipeLayout;
    }

    @Override
    public View generateView(final int position, View convertView, ViewGroup parent) {
        CheckWorkDetailListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.check_work_detail_list_item, parent, false);
        convertView = binding.getRoot();
        convertView.setTag(new ViewHolder(binding));

        return convertView;
    }

    @Override
    public void fillValues(final int position, View convertView) {
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        final CheckWorkDetailListItemBinding root = viewHolder.getBinding();
        root.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeAllExcept(root.swipeLayout);

                if (listener != null)
                    listener.onItemClick(position);
            }
        });

        root.btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onItemDelClick(position);
            }
        });
        root.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {

            @Override
            public void onStartOpen(SwipeLayout layout) {

            }

            @Override
            public void onOpen(SwipeLayout layout) {
                closeAllExcept(layout);
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onClose(SwipeLayout layout) {

            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });

        JSONObject item = items.getJSONObject(position);

        root.ASSESSMENTNAME.setText(StringUtil.getValue(item.getString("ASSESSMENT_NAME")));
        root.SCORE.setText(StringUtil.getValue(item.getString("SCORE")));
        root.OPINION.setText(StringUtil.getValue(item.getString("OPINION")));
        root.EDITNAME.setText(StringUtil.getValue(item.getString("EDITNAME")));
        root.EDITTIME.setText(StringUtil.toJSONTime(item.getString("EDITTIME")));
    }

    public void removeItem(int position) {
        closeAllItems();
        items.remove(position);
        notifyDataSetChanged();
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void addItem(JSONObject item) {
        closeAllItems();
        items.add(item);
        notifyDataSetChanged();
    }

    public void addItem(JSONObject item, int position) {
        closeAllItems();
        items.add(position, item);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void updateItem(JSONObject item) {
        for (int i = 0, j = items.size(); i < j; i++) {
            JSONObject _item = items.getJSONObject(i);
            if (StringUtil.dTs(item.getString("ID")).equals(StringUtil.dTs(_item.getString("ID")))) {
                _item.clear();
                _item.putAll(item);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(CheckWorkDetailListItemClickListener listener) {
        this.listener = listener;
    }

    private class ViewHolder {
        private CheckWorkDetailListItemBinding binding;

        public ViewHolder(CheckWorkDetailListItemBinding binding) {
            this.binding = binding;
        }

        public CheckWorkDetailListItemBinding getBinding() {
            return binding;
        }
    }
}
